package com.demo.modules.sys.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.demo.common.base.entity.ExcelVerifyEntity;
import com.demo.common.utils.CacheUtils;
import com.demo.common.validator.group.ImportGroup;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 系统用户
 */
@ExcelTarget("user")
@TableName("sys_user")
@Data
@Slf4j
public class SysUserEntity extends ExcelVerifyEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 姓名
     */
    @Excel(name = "姓名", width = 20, isImportField = "true_user")
    @NotNull(message = "姓名不能为空", groups = {ImportGroup.class})
    private String realName;
    /**
     * 手机号
     */
    @Excel(name = "手机号", width = 30, isImportField = "true_user")
    private String mobile;
    /**
     * 用户名
     */
    @Excel(name = "用户名", width = 20, isImportField = "true_user")
    @NotNull(message = "用户名不能为空", groups = {ImportGroup.class})
    private String username;
    /**
     * 密码
     */
    private String password;

    /**
     * 密码更新时间
     */
    @TableField(exist = false)
    private Date pwdUptTime;

    /**
     * 盐
     */
    private String salt;
    /**
     * 身份证号
     */
    @Excel(name = "身份证号", width = 30, isImportField = "true_user")
    private String idcard;
    /**
     * 邮箱
     */
    @Excel(name = "邮箱", width = 30, isImportField = "true_user")
    private String email;

    @Excel(name = "性别", replace = {"男_0", "女_1"}, width = 10, isImportField = "true_user")
    private Integer sex;

    @Excel(name = "出生日期", format = "yyyy-MM-dd", width = 30, isImportField = "true_user")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthDate;

    /**
     * 人员档案ID
     */
    private Integer memberId;

    /**
     * 状态  0：禁用   1：正常
     */
    @Excel(name = "状态", replace = {"禁用_0", "正常_1"}, width = 10)
    private Integer status;
    /**
     * 是否管理用户
     */
    private Integer isAdmin;
    /**
     * 系统预置
     */
    private Integer isSys;
    /**
     * 最后登录ip
     */
    private String lastLoginIp;
    /**
     * 最后登录时间
     */
    private Date lastLoginTime;
    /**
     * 微信openid
     */
    private String openid;

    /**
     * token  加密后
     */
    @TableField(exist = false)
    private String token;

    /**
     * 角色ID列表
     */
    @TableField(exist = false)
    private List<Long> roleIdList;

    /**
     * 所属集团ID
     */
    @TableField(exist = false)
    private Long groupId;

    @TableField(exist = false)
    private String groupName;

    /**
     * 所属公司ID
     */
    @TableField(exist = false)
    private Long corpId;

    /**
     * 所属公司名称
     */
    @TableField(exist = false)
    private String corpName;

    /**
     * 所属部门ID
     */
    @TableField(exist = false)
    private Long deptId;

    /**
     * 所属部门名称
     */
    @TableField(exist = false)
    private String deptName;

    /**
     * 所属岗位ID
     */
    @TableField(exist = false)
    private Long jobId;

    /**
     * 所属岗位名称
     */
    @TableField(exist = false)
    private String jobName;

    public String getGroupName() {
        return CacheUtils.getOrgCache(groupId).getOrgName();
    }

    public String getCorpName() {
        return CacheUtils.getOrgCache(corpId).getOrgName();
    }

    public String getDeptName() {
        return CacheUtils.getOrgCache(deptId).getOrgName();
    }

    public String getJobName() {
        return CacheUtils.getOrgCache(jobId).getOrgName();
    }

    public void setPassword(String password) {
        this.password = password;
        if (StrUtil.isNotBlank(password)) {
            this.pwdUptTime = new Date();
        }
    }

}
